package com.work.demo.file;

import lombok.Cleanup;

import java.io.*;

/**
 * @author LinYa
 */
public class BaseFileUtil {

	/**
	 * @Description: 获取流文件
	 * @Param: [ins, file]
	 * @return: void
	 * @Author: LinYa
	 * @Date: 2021/5/25
	 */

	public static void inputStreamToFile(InputStream ins, File file) {
		try {
			@Cleanup OutputStream os = new FileOutputStream(file);
			int bytesRead;
			byte[] buffer = new byte[8192];
			while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			ins.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 删除本地临时文件
	 *
	 * @param file
	 */
	public static void deleteTempFile(File file) {
		if (file != null) {
			File del = new File(file.toURI());
			del.delete();
		}

	}

	public static File createCsvFile(String filePath) throws IOException {
		File csvFile = null;

		csvFile = new File(filePath);

		File parentFile = csvFile.getParentFile();

		if (parentFile != null && !parentFile.exists()) {
			parentFile.mkdirs();
		}

		csvFile.createNewFile();

		return csvFile;
	}


}
